﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.DAL;
using Quartz;
using System;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading.Tasks;

namespace HamcoWcs.Workers
{

    [DisallowConcurrentExecution]
    public class NetWorker : WorkerBase
    {
        public NetWorker(WcsDbContext wcsDbContext, IMapper mapper, ILoggerService logger) : base(wcsDbContext, mapper, logger)
        {
        }

        public override Task Execute(IJobExecutionContext context)
        {
            var ipstr = "172.25.4.45";
            var a = PingNet(ipstr, out var status);


#if DEBUG
            Debug.Print($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 网络监控{a}[{ipstr}][{status}]");
#else
            if (!a)
            {
                _logger.AddSystemErrorLog($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 网络监控异常[{ipstr}][{status}]");
            }
#endif
            return Task.CompletedTask;
        }

        bool PingNet(string addr, out IPStatus status, int timeout = 3)
        {
            Ping p = new Ping();
            var pingReply = p.Send(IPAddress.Parse(addr), timeout);
            status = pingReply.Status;
            return (pingReply.Status == IPStatus.Success);
        }
    }
}
